package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.LeaveTypeManager;
import manager.UserManager;
import data.dao.LeaveDAOException;
import data.dao.UserDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dto.LeaveTypeDTO;
import data.dto.UserDTO;
import data.searchdto.UserSearchDTO;

/**
 * Servlet implementation class AdminStaffServlet
 */
@WebServlet("/AdminStaffServlet")
public class AdminStaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminStaffServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws UserDAOException,
			ServletException, IOException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (!util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			String type = request.getParameter("type") == null ? "" : request
					.getParameter("type").trim();
			page = "/admin/ViewPage.jsp?type=staff";
			String update = request.getParameter("update") == null ? ""
					: request.getParameter("update");
			String insert = request.getParameter("insert") == null ? ""
					: request.getParameter("insert");

			UserManager um = new UserManager();
			if (type.equalsIgnoreCase("delete")) {
				// do delete process here
				String staff = request.getParameter("id");
				Integer BI = new Integer(staff);
				int id = BI.intValue();
				UserDTO ut = new UserDTO();
				ut.setId(BI);

				try {
					um.deleteLeaveDetail(ut);
				} catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (data.dao.daoImplementation.SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (type.equalsIgnoreCase("insert")) {
				// do insert process here
				if (Short.parseShort(request.getParameter("userType")) > 0) {

					UserDTO ut = new UserDTO();
					ut.setUserType(Short.parseShort(request
							.getParameter("userType")));
					ut.setUserName(request.getParameter("userName"));
					// ut.setUserrank(Short.parseShort(request.getParameter("userRank")));
					ut.setPassword(request.getParameter("password"));
					ut.setName(request.getParameter("name"));
					ut.setAddress(request.getParameter("address"));
					ut.setPhone(request.getParameter("phone"));
					ut.setOverTime(Float.parseFloat(request
							.getParameter("overTime")));
					ut.setStatus((short) (1));
					// ut.setId(Integer.parseInt(request.getParameter("id")));
					um.insertLeaveDetail(ut);
					// ArrayList<UserDTO> data = um.findAllUser();
					// request.setAttribute("user", data);
				}
			} else if (type.equalsIgnoreCase("update")) {
				// do update process here
				if (Short.parseShort(request.getParameter("userType")) > 0) {

					UserDTO ut = new UserDTO();
					ut.setId(Integer.parseInt(request.getParameter("id")));
					ut.setUserType(Short.parseShort(request
							.getParameter("userType")));
					ut.setUserName(request.getParameter("userName"));
					// ut.setUserrank(Short.parseShort(request.getParameter("userRank")));
					ut.setPassword(request.getParameter("password"));
					ut.setName(request.getParameter("name"));
					ut.setAddress(request.getParameter("address"));
					ut.setPhone(request.getParameter("phone"));
					ut.setOverTime(Float.parseFloat(request
							.getParameter("overTime")));
					ut.setStatus(util.Constants.STATUS_SHOW);
					um.updateLeaveDetail(ut);
					// UserDTO data = um.findUser(user);
					// request.setAttribute("leaveType", data);
				}
			} else if (update.length() > 0) {
				UserSearchDTO userSearch = new UserSearchDTO();
				userSearch.setId(Short.parseShort(request.getParameter("id")));

				user = um.findUser(userSearch);

				request.setAttribute("currentuser", user);

				page = "/admin/adminStaffDetail.jsp";
			} else if (insert.length() > 0) {
				page = "/admin/adminStaffDetail.jsp";
			}

			request.setAttribute("user", um.findAllUser());
			request.setAttribute("userTypes", um.findAllUserTypeDisplay());
		}

		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);
	}

}
